package com.westos.gui;

import com.westos.codeaudit.catchcode.entity.VersionManage;
import com.westos.core.DoScan;
import com.westos.utils.JTextFieldHintListener;
import com.westos.utils.StartTimer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;

public class Index extends JFrame {

    // 定义组件
    JPanel jp1, jp2, jp3, jp4;
    JLabel jlb1, jlb2, jlb3;
    JButton jb1, jb2;
    JTextField jtf1, jtf2, jtf3;
    //JPasswordField jpf1;


    // 构造函数
    public Index() {

        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();

        jlb1 = new JLabel("文件ID");
        jlb2 = new JLabel("文件名");
        jlb3 = new JLabel("扫描方式");

        jb1 = new JButton("开始");
        jb2 = new JButton("取消");

        jtf1 = new JTextField(20);
        jtf2 = new JTextField(20);
        jtf3 = new JTextField(20);

        jtf3.addFocusListener(new JTextFieldHintListener("1代表sql,2代表xss", jtf3));
        //jpf1 = new JPasswordField(20);// 设置布局管理(上面忘记：extends JFrame，这里出错了)
        this.setLayout(new GridLayout(4, 1));

        // 加入各个组件
        jp1.add(jlb1);
        jp1.add(jtf1);
        jp2.add(jlb2);
        jp2.add(jtf2);
        jp3.add(jlb3);
        jp3.add(jtf3);
//        jp2.add(jlb2);
//        jp2.add(jpf1);

        jp4.add(jb1);
        jp4.add(jb2);

        // 加入到JFrame
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);

        this.setSize(500, 300);
        this.setTitle("代码审计");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        this.setBounds((screenSize.width - 500) / 2, (screenSize.height - 300) / 2, 500, 300);

        jb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int id = Integer.parseInt(jtf1.getText());
                    String dir = jtf2.getText();
                    String way = jtf3.getText();
                    //System.out.println(id + "==" + dir+"=="+way);
                    VersionManage versionManage = new VersionManage();
                    versionManage.setVerid(id);
                    versionManage.setPropath(dir);
                    versionManage.setScanway(way);
                    DoScan doScan = new DoScan(versionManage);
                    new StartTimer(doScan);
                    doScan.doScan();
                } catch (Exception ex) {
                    System.out.println("输入有误");
                }
            }
        });

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Index d1 = new Index();

    }
}
